import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Test {

    //三数之和
    public List<List<Integer>> threeSum(int[] nums) {
        int n = nums.length;
        List<List<Integer>> ret = new ArrayList<>();
        Arrays.sort(nums);
        for(int i=0;i<n;) {
            int left = i+1,right = n-1,target = -nums[i];
            while(left<right) {
                int sum = nums[left]+nums[right];
                if(sum<target) {
                    left++;
                }else if(sum>target) {
                    right--;
                }else {
                    List<Integer> tmp = new ArrayList<>();
                    tmp.add(nums[i]);
                    tmp.add(nums[left]);
                    tmp.add(nums[right]);
                    ret.add(tmp);
                    left++;
                    right--;
                    while(left<right&&nums[left-1]==nums[left]) {
                        left++;
                    }
                    while(left<right&&nums[right+1]==nums[right]) {
                        right--;
                    }
                }
            }
            i++;
            while(i<n&&nums[i-1]==nums[i]) {
                i++;
            }
        }
        return ret;
    }

    //四数之和
    public static List<List<Integer>> fourSum(int[] nums, int target) {
        Arrays.sort(nums);
        List<List<Integer>>ret = new ArrayList<>();
        int n = nums.length;
        for(int i=0;i<n;) {
            for(int j=i+1;j<n;){
                int _target = target - nums[i] - nums[j];
                int left = j+1,right = n-1;
                while(left<right) {
                    int sum = nums[left]+nums[right];
                    if(sum>_target) {
                        right--;
                    }else if(sum<_target) {
                        left++;
                    }else if(sum == _target){
                        List<Integer> tmp = new ArrayList<>();
                        System.out.println(_target+" "+sum);
                        tmp.add(nums[i]);tmp.add(nums[j]);tmp.add(nums[left]);tmp.add(nums[right]);
                        ret.add(tmp);
                        right--;
                        left++;
                        while(left<right && nums[left-1]==nums[left]) {
                            left++;
                        }
                        while(left<right && nums[right+1]==nums[right]) {
                            right--;
                        }
                    }
                }
                j++;
                while(j<n&&nums[j-1]==nums[j]) {
                    j++;
                }
            }
            i++;
            while(i<n&&nums[i-1]==nums[i]){
                i++;
            }
        }
        return ret;
    }

    public static int minSubArrayLen(int target, int[] nums) {
        int n = nums.length;
        int len = Integer.MAX_VALUE,sum = 0;
        for(int left = 0,right = 0;right<n;right++) {
            sum += nums[right];
            while(left >= 0 && sum > target) {
                sum -= nums[left];
                left++;
            }
            if(sum == target) {
                len = Math.min(len,right-left+1);
            }
        }
        return len==Integer.MAX_VALUE?0:len;
    }

    public static int longestOnes(int[] nums, int k) {
        int left = 0, right = 0, n = nums.length;
        int zeroNum = 0;
        int ret = 0;
        while(right<n) {
            while(right<n && zeroNum<k) {
                if(nums[right]==0) {
                    zeroNum++;
                }
                if(zeroNum==k) break;
                right++;
            }
            ret = Math.max(ret,right-left+1);
            while(zeroNum>=k) {
                if(nums[left]==0) {
                    zeroNum--;
                }
                left++;
            }
        }
        return ret;
    }

    public int totalFruit(int[] f) {
        int ret = 0;
        int hash[] = new int[100010];
        for(int left = 0, right = 0, kinds = 0; right<f.length; right++) {
            if(hash[f[right]]==0) {
                kinds++;
            }
            hash[f[right]]++;
            while(kinds>2) {
                hash[f[left]]--;
                if(hash[f[left]]==0) {
                    kinds--;
                }
                left++;
            }
            ret = Math.max(ret,right-left+1);
        }
        return ret;
    }

    public static void main(String[] args) {
        int ret = longestOnes(new int[]{1,1,1,0,0,0,1,1,1,1,0},2);
        System.out.println(ret);
    }
}
